/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author BAGUS
 */
public class Penerimaan {

    private String kodePenerimaan;
    private String waktuPenerimaan;
    private double jumlahBarang;
    private double hargaBarang;
    private String idBarang;
    private String username;
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "hr"; // gantikan dengan username anda
    String pass = "hr"; // gantikan dengan password anda

    /**
     * @return the kodePenerimaan
     */
    public String getKodePenerimaan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select kodepenerimaan from penerimaan order by kodepenerimaan");
            String kode_penerimaan = "";
            Date now = new Date();
            SimpleDateFormat dateFormatterTanggal = new SimpleDateFormat("d");
            SimpleDateFormat dateFormatterBulan = new SimpleDateFormat("M");
            SimpleDateFormat dateFormatterTahun = new SimpleDateFormat("y");
            String tanggal = dateFormatterTanggal.format(now);
            String bulan = dateFormatterBulan.format(now);
            String tahun = dateFormatterTahun.format(now);
            String substring = tahun.substring(2);
            String kodeTanggal = tanggal + bulan + substring;
            while (resultSet.next()) {
                kode_penerimaan = resultSet.getString("kodepenerimaan");
            }
            int no_penerimaan = 0;
            if ("".equals(kode_penerimaan)) {
                this.kodePenerimaan = kodeTanggal + "101";
            } else {
                String data = null;
                if (kode_penerimaan.length() == 9) {
                    data = kode_penerimaan.substring(0, 6);
                } else {
                    data = kode_penerimaan.substring(0, 5);
                }
                if (kodeTanggal.equals(data)) {
                    no_penerimaan = Integer.parseInt(kode_penerimaan) + 1;
                    this.kodePenerimaan = String.valueOf(no_penerimaan);
                } else {
                    this.kodePenerimaan = kodeTanggal + "101";
                }
            }
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
            this.kodePenerimaan = null;
        }
        return kodePenerimaan;
    }

    /**
     * @return the waktuPenerimaan
     */
    public String getWaktuPenerimaan() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d-M-y");
        this.waktuPenerimaan = simpleDateFormat.format(date);
        return waktuPenerimaan;
    }

    /**
     * @return the jumlahBarang
     */
    public double getJumlahBarang() {
        return jumlahBarang;
    }

    /**
     * @param jumlahBarang the jumlahBarang to set
     */
    public void setJumlahBarang(double jumlahBarang) {
        this.jumlahBarang = jumlahBarang;
    }

    /**
     * @return the hargaBarang
     */
    public double getHargaBarang() {
        return hargaBarang;
    }

    /**
     * @param hargaBarang the hargaBarang to set
     */
    public void setHargaBarang(double hargaBarang) {
        this.hargaBarang = hargaBarang;
    }

    /**
     * @return the idBarang
     */
    public String getIdBarang() {
        return idBarang;
    }

    /**
     * @param idBarang the idBarang to set
     */
    public void setIdBarang(String idBarang) {
        this.idBarang = idBarang;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    public void insertPenerimaan() {
        try {
            Connection connection = null;
            OracleDataSource ds;
            ds = new OracleDataSource();
            ds.setURL(jdbcUrl);
            ds.setPassword(pass);
            ds.setUser(userid);
            connection = (Connection) ds.getConnection();
            Statement statement = connection.createStatement();
            statement.executeQuery("insert into penerimaan values ('"
                    + getKodePenerimaan() + "','"
                    + getWaktuPenerimaan() + "',"
                    + getJumlahBarang() + ","
                    + getHargaBarang() + ",'"
                    + getIdBarang() + "','"
                    + getUsername() + "')");
            BarangGudang barangGudang = new BarangGudang();
            barangGudang.setIdBarang(getIdBarang());
            barangGudang.setJumlahBarang(getJumlahBarang());
            barangGudang.setHargaBarang(getHargaBarang());
            barangGudang.tambahBarang(barangGudang);
            barangGudang.updateHargaBarangGudang(barangGudang);
        } catch (SQLException e) {
// perform error handling here
            System.out.println("error :" + e.getMessage());
        }
    }
}
